# Create your views here.

from django.http import HttpResponse
from django.http import HttpResponseRedirect

from django.utils import simplejson

from market.playerData import PlayerData
from market.marketData import MarketData
from market.jsonsForCharts import createPricesChartJson
from market.jsonsForCharts import createMtMsChartJson
from market.jsonsForCharts import getMainViewInMtMJson
from market.jsonsForCharts import createRankingTableJson
from market.jsonsForCharts import getJsonPlayerMainViewQty

#keep djando happy (suppress and try to access /Admin/)
def jsonMarketView(request):
    # authentification
	if not request.user.is_authenticated():
		return HttpResponseRedirect('/login/?next=%s' % request.path)

	m = MarketData();
	v = m.getMarketView()
	return HttpResponse(simplejson.dumps(v.toDictionary()), mimetype='application/javascript')

# return a json with the percentage (in number of asset) of the player
def jsonPlayerMainView(request):

    # authentification
	if not request.user.is_authenticated():
		return HttpResponseRedirect('/login/?next=%s' % request.path)

    # the user is here authentified - we get the matching player
	p = PlayerData(request.user.username)
	if( not p.isListedPlayer ):
		msg = '%s is not a listed player' %(request.user.username)
		return HttpResponse( msg )

	v = p.getViewMainFund()
	return HttpResponse(simplejson.dumps(v.toDictionary()), mimetype='application/javascript')

def jsonPlayerMainViewQty(request):
	# authentification
	if not request.user.is_authenticated():
		return HttpResponseRedirect('/login/?next=%s' % request.path)

	p = PlayerData(request.user.username)
	return HttpResponse(getJsonPlayerMainViewQty(p), mimetype='application/javascript')

# return a json with the percentage (in mtm of asset) of the player
def jsonPlayerMainViewInMtM(request):

    # authentification
	if not request.user.is_authenticated():
		return HttpResponseRedirect('/login/?next=%s' % request.path)

    # the user is here authentified - we get the matching player
	p = PlayerData(request.user.username)
	if( not p.isListedPlayer ):
		msg = '%s is not a listed player' %(request.user.username)
		return HttpResponse( msg )

        return HttpResponse(getMainViewInMtMJson(p), mimetype='application/javascript')


# return a json with the history of prices per asset
def jsonPricesHistory(request):
	# no authentification requiered here
	return HttpResponse(createPricesChartJson(), mimetype='application/javascript')

# return a json with the history of mtm per player
def jsonMtMsHistory(request):
	# no authentification requiered here
	return HttpResponse(createMtMsChartJson(), mimetype='application/javascript')


# return a json with the ranking in case of asset strike
def jsonGetRanking(request):
	# no authentification requiered here
	return HttpResponse(createRankingTableJson(), mimetype='application/javascript')
